package com.macbulldev.poc.testgin.controller;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.macbulldev.poc.testgin.calculations.CalcUtils;
import com.macbulldev.poc.testgin.domain.Scenario;
import com.macbulldev.poc.testgin.domain.TrialCase;
import com.macbulldev.poc.testgin.domain.TrialGroup;


@org.springframework.roo.addon.web.mvc.controller.RooWebScaffold(path = "trialcase", automaticallyMaintainView = false, formBackingObject = com.macbulldev.poc.testgin.domain.TrialCase.class)
@org.springframework.web.bind.annotation.RequestMapping("/trialcase/**")
@org.springframework.stereotype.Controller
public class TrialCaseController {
	
	private static final Logger LOGGER = Logger.getLogger(TrialCaseController.class);
	
	@Transactional
	@RequestMapping(value = "/trialcase/ajax/create_trialcase/{id}", method = RequestMethod.POST)      
    public String processCreateTrialCase(@PathVariable("id") Long scenarioId,ModelMap modelMap,HttpSession session) 
	{

		Scenario s = Scenario.findScenario(scenarioId);
		
		LOGGER.error("Call the Gateway Controller for :  "+s.getPolicyType().getAbbrName());
		
		CalcUtils.calculationGateway(s.getPolicyType(), s);
		
		LOGGER.error(s.getTrialGroups().size()+"  - TG Size");
		modelMap.addAttribute("scenario",s);
		
		return "scenario/ajax_show";
	}
	

	@RequestMapping(value = "/trialcase", method = RequestMethod.POST)    
    public String create(@Valid TrialCase trialCase, BindingResult result, ModelMap modelMap) {    
        if (trialCase == null) throw new IllegalArgumentException("A trialCase is required");        
        if (result.hasErrors()) {        
            modelMap.addAttribute("trialCase", trialCase);            
            modelMap.addAttribute("trialgroups", TrialGroup.findAllTrialGroups());            
            return "trialcase/create";            
        }        
        trialCase.persist();        
        return "redirect:/trialcase/" + trialCase.getId();        
    }

	@RequestMapping(value = "/trialcase/form", method = RequestMethod.GET)    
    public String createForm(ModelMap modelMap) {    
        modelMap.addAttribute("trialCase", new TrialCase());        
        modelMap.addAttribute("trialgroups", TrialGroup.findAllTrialGroups());        
        return "trialcase/create";        
    }

	@RequestMapping(value = "/trialcase/{id}", method = RequestMethod.GET)    
    public String show(@PathVariable("id") Long id, ModelMap modelMap) {    
        if (id == null) throw new IllegalArgumentException("An Identifier is required");        
        modelMap.addAttribute("trialCase", TrialCase.findTrialCase(id));        
        return "trialcase/show";        
    }

	@RequestMapping(value = "/trialcase", method = RequestMethod.GET)    
    public String list(@RequestParam(value = "page", required = false) Integer page, @RequestParam(value = "size", required = false) Integer size, ModelMap modelMap) {    
        if (page != null || size != null) {        
            int sizeNo = size == null ? 10 : size.intValue();            
            modelMap.addAttribute("trialcases", TrialCase.findTrialCaseEntries(page == null ? 0 : (page.intValue() - 1) * sizeNo, sizeNo));            
            float nrOfPages = (float) TrialCase.countTrialCases() / sizeNo;            
            modelMap.addAttribute("maxPages", (int) ((nrOfPages > (int) nrOfPages || nrOfPages == 0.0) ? nrOfPages + 1 : nrOfPages));            
        } else {        
            modelMap.addAttribute("trialcases", TrialCase.findAllTrialCases());            
        }        
        return "trialcase/list";        
    }

	@RequestMapping(method = RequestMethod.PUT)    
    public String update(@Valid TrialCase trialCase, BindingResult result, ModelMap modelMap) {    
        if (trialCase == null) throw new IllegalArgumentException("A trialCase is required");        
        if (result.hasErrors()) {        
            modelMap.addAttribute("trialCase", trialCase);            
            modelMap.addAttribute("trialgroups", TrialGroup.findAllTrialGroups());            
            return "trialcase/update";            
        }        
        trialCase.merge();        
        return "redirect:/trialcase/" + trialCase.getId();        
    }

	@RequestMapping(value = "/trialcase/{id}/form", method = RequestMethod.GET)    
    public String updateForm(@PathVariable("id") Long id, ModelMap modelMap) {    
        if (id == null) throw new IllegalArgumentException("An Identifier is required");        
        modelMap.addAttribute("trialCase", TrialCase.findTrialCase(id));        
        modelMap.addAttribute("trialgroups", TrialGroup.findAllTrialGroups());        
        return "trialcase/update";        
    }

	@RequestMapping(value = "/trialcase/{id}", method = RequestMethod.DELETE)    
    public String delete(@PathVariable("id") Long id, @RequestParam(value = "page", required = false) Integer page, @RequestParam(value = "size", required = false) Integer size) {    
        if (id == null) throw new IllegalArgumentException("An Identifier is required");        
        TrialCase.findTrialCase(id).remove();        
        return "redirect:/trialcase?page=" + ((page == null) ? "1" : page.toString()) + "&size=" + ((size == null) ? "10" : size.toString());        
    }
}
